<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
use app\common\model\Uploads as UploadsModel;
use think\facade\Filesystem;
use think\facade\Log;

class Uploads extends AdminBase
{
    /**
     * 上传图片
     * @return array|\think\response\Json
     */
    public function index()
    {
        try {
            $file = request()->file('file');
            //处理图片
            if(substr($file->getOriginalMime(),0,5) == 'image'){
                UploadsModel::UploadValidate($file);
            }
            $params = get_system('storage');
            //判断上传位置
            if($params['type']=='qiniu'){//七牛
                $key = 'admin_'.date('Y/md/His_').substr(microtime(), 2, 6).'_'.mt_rand(0,999).'.'.$file->getOriginalExtension();
                $qiniu = new \app\common\library\Qiniu();
                $url = $params['domain'].$qiniu->upload($file->getRealPath(), $key);
                UploadsModel::CreateInfoAdmin('qiniu',$params['domain'], $key,$file->getOriginalName(), $file->getSize(), $file->getOriginalMime());
            }else{//默认本地
                $savename = Filesystem::disk('public')->putFile('uploads/admin',$file);
                $url = request()->domain().'/'.$savename;
                UploadsModel::CreateInfoAdmin('local','', $savename,$file->getOriginalName(), $file->getSize(), $file->getOriginalMime());
            }
            return json([
                'name' => $file->getFilename(),
                'status' => 'done',
                'url' => $url,
                'thumbUrl' => $url,
            ]);
        } catch (\Exception $e) {
            return ['code' => 0, 'msg' => $e->getMessage()];
        }
    }
    /**
     * 上传文件
     * @return array|\think\response\Json
     */
    public function file()
    {
        try {
            $file = request()->file('file');
            //处理图片
            if(substr($file->getOriginalMime(),0,5) == 'image'){
                UploadsModel::UploadValidate($file);
            }
            $params = get_system('storage');
            //判断上传位置
            if($params['type']=='qiniu'){//七牛
                $key = 'admin_'.date('Ym').'/'.$file->getOriginalName().'.'.$file->getOriginalExtension();
                $qiniu = new \app\common\library\Qiniu();
                $url = $params['domain'].$qiniu->upload($file->getRealPath(), $key);
                UploadsModel::CreateInfoAdmin('qiniu',$params['domain'], $key,$file->getOriginalName(), $file->getSize(), $file->getOriginalMime());
            }else{//默认本地
                $savename = Filesystem::disk('public')->putFileAs('uploads/'.date('Ym'),$file,$file->getOriginalName());
                $url = request()->domain().'/'.$savename;
                UploadsModel::CreateInfoAdmin('local','', $savename,$file->getOriginalName(), $file->getSize(), $file->getOriginalMime());
            }
            return json([
                'name' => $file->getFilename(),
                'status' => 'done',
                'url' => $url,
                'thumbUrl' => $url,
            ]);
        } catch (\Exception $e) {
            return ['code' => 0, 'msg' => $e->getMessage()];
        }
    }

}
